﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework
 * Version 2.10
 * 
 * POP3 Authorization Session Class
 *      Implements the authorization session/state section of the POP3 protocol.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using Adaptive.Foundation;
using Adaptive.Foundation.Network.Cnr;
using Adaptive.Network.Sockets;

namespace Adaptive.Foundation.Network.Pop3
{
    /// <summary>
    /// Implements the authorization session/state section of the POP3 protocol.
    /// </summary>
    public class AuthorizationSession : CnrAuthenticationSession  
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Client used for communications.
        /// </summary>
        private SocketsClient _client;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AuthorizationSession(SocketsClient client)
            : base()
        {
            _client = client;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
            }
            _client = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        /// <summary>
        /// Attempts to authenticate the user on a POP3 remote host.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the remote host indicates the user name is valid; otherwise, <b>false</b>.
        /// </returns>
        protected override bool PerformUserIdentification()
        {
            UserCommand command = null;
            Pop3Response response = null;

            command = new UserCommand(_client, Credentials.UserIdentityValue);
            ((CnrCommandParameter<string>)command.Parameters["userId"]).DataContent = Credentials.UserIdentityValue;
            response = (Pop3Response)command.Execute();
            return response.Success;
        }
        /// <summary>
        /// Attempts to authenticate the user's password on a POP3 remote host.
        /// </summary>
        /// <returns>
        /// <b>true</b> if the remote host indicates the user password is valid; otherwise, <b>false</b>.
        /// </returns>
        protected override bool PerformSecurityValidation()
        {
            PassCommand command = null;
            Pop3Response response = null;

            command = new PassCommand(_client, Credentials.SecurityValue);
            response = (Pop3Response)command.Execute();
            return response.Success;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion
    }
}
